/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package xmlutils;

import DTO.album.AlbumType;
import DTO.album.Albums;
import DTO.album.ArtistType;
import DTO.album.ArtistsType;
import DTO.album.SongType;
import DTO.album.SongsType;
import java.math.BigInteger;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

/**
 *
 * @author NoiR
 */
public class SearchAlbumSAXHandler extends DefaultHandler {

    private Albums albums;
    private String tag;
    private AlbumType album;
    private SongType song;
    private SongsType songs;
    private boolean va;
    private ArtistsType artists;
    private ArtistType artist;
    private boolean found;
    private String targetid;
    private boolean isGotID;
    private boolean isGotName;

    public SearchAlbumSAXHandler() {
        this.tag = "";
        this.va = false;
        this.album = null;
        this.found = false;
        this.isGotID = false;
        this.isGotName = false;
    }

    public SearchAlbumSAXHandler(String albumID) {
        this.tag = "";
        this.va = false;
        this.album = null;
        this.found = false;
        this.targetid = albumID;
        this.isGotID = false;
        this.isGotName = false;
    }

    public Albums getAlbums() {
        return albums;
    }

    @Override
    public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
        tag = qName;

        if ("Album".equals(qName)) {
            album = new AlbumType();
        }
        if (found) {
            if ("Songs".equals(qName)) {
                songs = new SongsType();
            } else if ("Song".equals(qName)) {
                song = new SongType();
            }
            // if various artist then add
            if (va) {
                if ("Artists".equals(qName)) {
                    artists = new ArtistsType();
                } else if ("Artist".equals(qName)) {
                    artist = new ArtistType();
                }
            } // end various artist
        }
    }

    @Override
    public void endElement(String uri, String localName, String qName) throws SAXException {

        if (found) {
            if ("Album".equals(qName)) {
                if (album != null) {
                    albums = new Albums();
                    albums.getAlbum().add(album);
                    found = false;
                }
            } else if ("Song".equals(qName)) {
                if (song != null) {
                    songs.getSong().add(song);
                }
            } else if ("Artist".equals(qName)) {
                if (artist != null) {
                    artists.getArtist().add(artist);
                }
            } else if ("Songs".equals(qName)) {
                if (songs != null) {
                    album.setSongs(songs);
                }
            } else if ("Artists".equals(qName)) {
                if (artists != null) {
                    song.setArtists(artists);
                }
            }
        }

        tag = "";
    }

    @Override
    public void characters(char[] ch, int start, int length) throws SAXException {
        String content = new String(ch, start, length);

        // add album information
        if ("AlbumID".equals(tag)) {
            if (content.trim().equals(targetid)) {
                found = true;
                album.setAlbumID(BigInteger.valueOf(Integer.parseInt(content.trim())));
            }
        }
        if (found) {
            if ("AlbumTitle".equals(tag)) {
                album.setAlbumTitle(content.trim());
            } else if ("ArtistID".equals(tag)) {
                if (!isGotID) {
                    album.setArtistID(BigInteger.valueOf(Integer.parseInt(content.trim())));
                    isGotID = true;
                }
            } else if ("ArtistName".equals(tag)) {
                if (!isGotName) {
                    album.setArtistName(content.trim());
                    isGotName = true;
                }
            } else if ("AlbumCoverSRC".equals(tag)) {
                album.setAlbumCoverSRC(content.trim());
            } else if ("AlbumViews".equals(tag)) {
                album.setAlbumViews(BigInteger.valueOf(Integer.parseInt(content.trim())));
            } else if ("AlbumCoverSRC".equals(tag)) {
                album.setAlbumCoverSRC(content.trim());
            } else if ("AlbumDescription".equals(tag)) {
                album.setAlbumDescription(content.trim());
            } else if ("VariousArtist".equals(tag)) {
                va = "true".equals(content.trim());
                album.setVariousArtist(va);
            } // add song in album
            else if ("SongID".equals(tag)) {
                song.setSongID(BigInteger.valueOf(Integer.parseInt(content.trim())));
            } else if ("SongTitle".equals(tag)) {
                song.setSongTitle(content.trim());
            } else if ("SongSRC".equals(tag)) {
                song.setSongSRC(content.trim());
            } else if ("Lyrics".equals(tag)) {
                song.setLyrics(content.trim());
            } else if ("OrderNumber".equals(tag)) {
                song.setOrderNumber(BigInteger.valueOf(Integer.parseInt(content.trim())));
            }
            // add artist information if va = true;
            if (va) {
                if ("ArtistID".equals(tag)) {
                    artist.setArtistID(BigInteger.valueOf(Integer.parseInt(content.trim())));
                } else if ("ArtistName".equals(tag)) {
                    artist.setArtistName(content.trim());
                }
            }
            // end
        }

    }
}
